﻿using FictionsDownload.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FictionsDownload.UI
{
    public partial class FormTitleBar : Form
    {
        #region 内部变量
        private FormMain FormMain
        {
            get
            {
                if (this.MdiParent == null)
                {
                    //父窗体不是FormMain，是Panel
                    return (FormMain)this.Parent.Parent;
                }
                else
                {
                    return (FormMain)this.MdiParent;
                }
            }
        }
        public FormQidian formQidian = new FormQidian();
        #endregion

        #region 单例初始化
        private FormTitleBar()
        {
            InitializeComponent();
            //
            this.TopLevel = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Show();
            //
        }
        private static FormTitleBar Instance;
        private static readonly object lockRoot = new object();
        /// <summary>
        /// 初始化实例
        /// </summary>
        public static FormTitleBar GetIntance
        {
            get
            {
                if (Instance == null)
                {
                    lock (lockRoot)
                    {
                        if (Instance == null)
                        {
                            Instance = new FormTitleBar();
                        }
                    }
                }
                return Instance;
            }
        }
        #endregion
        #region 控件初始化
        /// <summary>
        /// 窗口初始化之后，控件再做调整
        /// </summary>
        public void InitializeComponentCode()
        {
            pbox_TitleInit();
            FormMain.TilteBarHeight = this.ClientSize.Height;
            formQidianInit();
        }
        private void formQidianInit()
        {
            formQidian.UserNameChanged += new EventHandler(QidianUserNameChange);
            formQidian.WindowState = FormWindowState.Minimized;
            formQidian.ShowInTaskbar = false;
            formQidian.Show();
            formQidian.Hide();
            formQidian.AutoWeb();
        }
        private void pbox_TitleInit()
        {
            pbox_Title.Location = new Point(0, 0);
            pbox_Title.Size = new Size(this.Size.Height, this.Size.Height);
        }
        #endregion

        #region 标题栏移动事件
        private bool isMouseDown = false;
        private Point cwpt = new Point(); //当前窗体位置
        private Point cmpt = new Point(); //当前鼠标的按下位置
        private void FormTitleBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                cwpt = FormMain.Location;
                cmpt = System.Windows.Forms.Control.MousePosition;
            }
        }
        private void FormTitleBar_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }
        private void FormTitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            int rangeX = 0, rangeY = 0; //计算当前鼠标光标的位移，让窗体进行相同大小的位移
            if (isMouseDown)
            {
                Point pt = System.Windows.Forms.Control.MousePosition;
                rangeX = cmpt.X - pt.X;
                rangeY = cmpt.Y - pt.Y;
                FormMain.Location = new Point(cwpt.X - rangeX, cwpt.Y - rangeY);
            }
        }
        #endregion

        #region 最大、最小、关闭按钮
        private void btn_Close_Click(object sender, EventArgs e)
        {
            DialogResult isExit = MessageBox.Show(
                "您确定要退出程序吗？",
                "确认退出", 
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question, 
                MessageBoxDefaultButton.Button2
                );
            if (isExit == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void btn_Max_Click(object sender, EventArgs e)
        {
            FormMainMaxOrNormal();
        }

        private void btn_Min_Click(object sender, EventArgs e)
        {
            FormMain.WindowState = FormWindowState.Minimized;
        }
        /// <summary>
        /// FormMain窗口变化：最大或正常
        /// </summary>
        private void FormMainMaxOrNormal()
        {
            if (FormMain.WindowState == FormWindowState.Maximized)
            {
                FormMain.WindowState = FormWindowState.Normal;
            }
            else
            {
                FormMain.WindowState = FormWindowState.Maximized;
            }
        }

        private void FormTitleBar_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            FormMainMaxOrNormal();
        }
        #endregion

        private void 正文匹配设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormWebFormatList formWebFormatList = new FormWebFormatList();
            formWebFormatList.ShowDialog();
        }

        private void btn_Menu_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                cms_Menu.Show((Button)sender, new Point(e.X, e.Y)); //有的换位button1
            }
        }

        private void btnQidian_Click(object sender, EventArgs e)
        {
            if (!formQidian.ShowInTaskbar)
            {
                formQidian.ShowInTaskbar = true;
            }
            if (formQidian.WindowState == FormWindowState.Minimized)
            {
                formQidian.WindowState = FormWindowState.Maximized;
            }
            formQidian.Show();
        }
        private void QidianUserNameChange(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler(QidianUserNameChange), sender, e);
                return;
            }
            TextEventArgs te = (TextEventArgs)e;
            btnQidian.Text = te.Text;
        }
    }
}
